package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.*;
import com.suyuan.entity.*;
import com.suyuan.service.NewTransitBatchService;
import com.suyuan.service.StationService;
import com.suyuan.service.TransitBatchInfoService;
import com.suyuan.vo.NewTransitBatchVO;
import com.suyuan.vo.TransitBatchInfoVO;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 物流批次更新后的列表
 */
@Service(value = "newTransitBatchService")
@Transactional
public class NewTransitBatchServiceImpl implements NewTransitBatchService {

    @Resource
    private NewtransitbatchMapper newtransitbatchMapper;
    @Resource
    private TransportrouteInforMapper transportrouteInforMapper;
    @Resource
    private CarinfoMapper carinfoMapper;
    @Resource
    private CompanyInfoMapper companyInfoMapper;
    @Resource
    private TransitInfoMapper transitMapper;
    @Resource
    private StationMapper stationMapper;

    /**
     * 线路批次列表
     * @return
     */
    public List<NewTransitBatchVO> list() {
        List<NewTransitBatchVO> newBatchVOs = new ArrayList<NewTransitBatchVO>();

        NewtransitbatchExample example = new NewtransitbatchExample();
        List<Newtransitbatch> batches = newtransitbatchMapper.selectByExample(example);

        for(Newtransitbatch batchInfo : batches ){
            //路线
            TransportRouteInfor route = transportrouteInforMapper.selectByPrimaryKey(batchInfo.getRouteid());
            //货车
            CarInfo car = carinfoMapper.selectByPrimaryKey(batchInfo.getCarid());
            //物流公司
            CompanyInfo company = companyInfoMapper.selectByPrimaryKey(batchInfo.getCompid());
            //添加进VO集合中
            newBatchVOs.add(new NewTransitBatchVO(batchInfo,route,company,car));
        }
        return newBatchVOs;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id) {
        //先删除该id绑定的所有外键表，transit_infor物流信息
        NewtransitbatchExample example = new NewtransitbatchExample();
        NewtransitbatchExample.Criteria criteria = example.createCriteria();
        criteria.andTransitbatchidEqualTo(id);
        newtransitbatchMapper.deleteByExample(example);
        //删除批次
        int code = newtransitbatchMapper.deleteByPrimaryKey(id);
        return code;
    }

    /**
     * 插入
     * @param newtransitbatch
     * @return
     */
    public int insert(Newtransitbatch newtransitbatch) {
        int code = newtransitbatchMapper.insertSelective(newtransitbatch);
        return code;
    }

    /**
     * 线路集合
     */
    public List<TransportRouteInfor> getRoutes(){
        return transportrouteInforMapper.selectByExample(new TransportRouteInforExample());
    }

    /**
     * 货车集合
     */
    public List<CarInfo> getCars(){
        return carinfoMapper.selectByExample(new CarInfoExample());
    }

    /**
     * 物流公司集合
     */
    public List<CompanyInfo> getCompanies(){
        return companyInfoMapper.selectByExample(new CompanyInfoExample());
    }

    /**
     * 条件、分页线路列表
     * @param batchId
     * @param routeId
     * @param page
     * @return
     */
    public PageInfo inquiry(String batchId, String routeId, Page page) {
        List<NewTransitBatchVO> vos = new ArrayList<NewTransitBatchVO>();
        if(StringUtils.isBlank(batchId)){
            batchId = null;
        }
        if(StringUtils.isBlank(routeId)){
            routeId = null;
        }

        //批次信息
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<Newtransitbatch> batches = newtransitbatchMapper.likeInquiry(batchId,routeId);
        PageInfo info = new PageInfo(batches);
        for(Newtransitbatch batch : (List<Newtransitbatch>)info.getList()){
            TransportRouteInfor route = transportrouteInforMapper.selectByPrimaryKey(batch.getRouteid());
            CarInfo car = carinfoMapper.selectByPrimaryKey(batch.getCarid());
            CompanyInfo company = companyInfoMapper.selectByPrimaryKey(batch.getCompid());
            vos.add(new NewTransitBatchVO(batch,route,company,car));
        }
        info.setList(vos);
        return info;
    }

    public Newtransitbatch findById(Integer id) {
        Newtransitbatch batch = newtransitbatchMapper.selectByPrimaryKey(id);
        return batch;
    }

    public int update(Newtransitbatch batchInfo) {
        return newtransitbatchMapper.updateByPrimaryKey(batchInfo);
    }

    public List<Station> getStations(Integer routeId) {
        StationExample example = new StationExample();
        StationExample.Criteria criteria = example.createCriteria();
        criteria.andRouteidEqualTo(routeId);
        return stationMapper.selectByExample(example);
    }

    public Station getStationById(Integer startstationid) {
        Station station = stationMapper.selectByPrimaryKey(startstationid);
        return station;
    }
}
